package org.vector.view;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import org.vector.serviceImpl.BankServiceImpl;
import org.vector.utils.BaseDao;

@SuppressWarnings("serial")
public class WindowTransfer extends JFrame{
	BaseDao bd = new BaseDao();
	BankServiceImpl bsi = new BankServiceImpl();
	private JTextField t1,t2;
	private JPasswordField ps;
	private JLabel l1,l2,l3,l4;
	private JButton btn1,btn2;
	
	public WindowTransfer() {
	}
	public WindowTransfer(int bankid) {
		into(bankid);
		setVisible(true);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}

	private void into(int bankid) {
		setSize(300,500);
		setLocationRelativeTo(null);
		setLayout(new FlowLayout());
		l1 = new JLabel("转账");
		add(l1);
		
		//p = new JPanel();
		//p.setLayout(new FlowLayout());
		l2 = new JLabel("转账金额");
		t1 = new JTextField(20);
		l3 = new JLabel("支付密码");
		ps = new JPasswordField(20);
		l4 = new JLabel("目标账号");
		t2 = new JTextField(20);
		
		add(l2);
		add(t1);
		add(l3);
		add(ps);
		add(l4);
		add(t2);
		//add(p,BorderLayout.CENTER);
		
		//down = new JPanel();
		btn1 = new JButton("取消");
		btn2 = new JButton("确定");
		add(btn1);
		add(btn2);
		
		btn2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				String money1 = t1.getText();
				String pass = new String(ps.getPassword());
				String bankid2 = t2.getText();
				int a = Integer.valueOf(money1);
				int b = Integer.valueOf(pass);
				int c = Integer.valueOf(bankid2);
				
				String sql2 = "select bankid from bank where bankid = ?";
				ResultSet r = bd.executeQuery(sql2, c);
				try {
					if(r.next()) {
						int row = bsi.transfer(b, a+3, bankid, c);
						if(row == 1) {
							WindowTransfer.this.dispose();
							Date date = new Date();
							SimpleDateFormat sdf =
									new SimpleDateFormat("yyMMddHHmm");
							String s = sdf.format(date);
							int gouwuid = (int)(Math.random()*(10000-1000+1)+1000);
							bsi.savedaytoday(gouwuid, bankid, c, a, 3, "转账", s);
						}else {
							new Wcuowu("密码错误");
						}
					}else {
						new Wcuowu("没有该账号");
					}
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		});
		btn1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				WindowTransfer.this.dispose();
			}
		});
		
	}
}
